<template>
  <div class="app-container calendar-list-container">
    <div v-if="visible">
      <el-form :model="entity" :rules="dataRule" ref="entity" label-width="80px" size="small">
        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="entity.companyName" placeholder="名称" :disabled="view"></el-input>
        </el-form-item>
        <el-form-item label="统一社会信用代码" prop="creditCode">
          <el-input v-model="entity.creditCode" placeholder="统一社会信用代码" :disabled="view"></el-input>
        </el-form-item>
        <el-form-item label="手机号码" prop="mobile">
          <el-input v-model="entity.mobile" placeholder="手机号码" :disabled="view"></el-input>
        </el-form-item>
        <el-form-item label="法人信息" prop="legalPerson">
          <el-input v-model="entity.legalPerson" placeholder="法人信息" :disabled="view"></el-input>
        </el-form-item>
        <el-form-item label="公司地址" prop="address">
          <el-input v-model="entity.address" placeholder="公司地址" :disabled="view"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <div class="dialog-footer-div">
          <el-button @click="cancel()">取消</el-button>
          <el-button v-if="!view" type="primary" @click="submit()">确定</el-button>
        </div>
      </span>
    </div>
  </div>
</template>

<script>
import request from '@/router/axios'
export default {
  data () {
    return {
      view: false,
      url: '',
      entity: {
        appName: '',
        appKey: ''
      },
      dataRule: {
        companyName: [
          { required: true, message: '公司名称不能为空', trigger: 'blur' },
          { min: 2, message: '长度在不能小于2', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '联系电话不能为空', trigger: 'blur' },
          { min: 2, message: '长度在不能小于2', trigger: 'blur' }
        ],
      }
    }
  },
  props: {
    visible: {
      type: Boolean,
      default: function () {
        return false
      }
    }
  },
  methods: {
    init (title, entity, view, url) {
      this.$nextTick(() => {
        this.title = title
        this.$refs['entity'].resetFields()
        this.view = view
        this.entity = entity
        this.url = url
      })
    },
    cancel () {
      this.$emit('cancel')
    },
    submit () {
      this.$refs['entity'].validate((valid) => {
        if (valid) {
          this.handleUrl(this.entity).then((data) => {
            if (data) {
              this.$emit('cancel')
            }
          })
        }
      })
    },
    handleUrl (data) {
      return request({
        url: this.url,
        method: 'post',
        data
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>
